package test;

import java.util.List;

import org.android.framework.R;
import org.zw.android.framework.async.AsyncTaskExecutor;
import org.zw.android.framework.async.AsyncTaskHandler;
import org.zw.android.framework.async.AsyncTaskRunnable;
import org.zw.android.framework.cache.ImageOption;
import org.zw.android.framework.impl.ImageCooler;
import org.zw.android.framework.scaner.FileScaner;
import org.zw.android.framework.scaner.ImageFile;

import android.content.Context;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ImageListView extends ListView {
	
	List<ImageFile> list ;
	
	LayoutInflater mInflater ;
	
//	ImageDownloader downloader ;
	ImageOption op = ImageOption.defOption ;
	
	public ImageListView(Context context) {
		this(context, null);
	}

	public ImageListView(final Context context, AttributeSet attrs) {
		super(context, attrs);
		
		op.setFadeIn(true);
//		downloader = ImageDownloader.open(context, "thumh", 2 * 1024 * 1024, 640, 960, 1);
		
		setAdapter(adapter);
		
		mInflater = LayoutInflater.from(context);
		
		setOnScrollListener(new OnScrollListener() {
			
			@Override
			public void onScrollStateChanged(AbsListView arg0, int scrollState) {
				switch (scrollState) {  
	            case OnScrollListener.SCROLL_STATE_IDLE: //  
	            	ImageCooler.onResume() ;
	            	// downloader.onResume() ;
	            	break;  
	            case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:  
	            case OnScrollListener.SCROLL_STATE_FLING:  
	            	ImageCooler.onPause() ;
	            	//downloader.onPause() ;
	                break;  
	            }  
			}
			
			@Override
			public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
				
			}
		}) ;
	}
	
	public void onResume(){
		
		AsyncTaskHandler handler = new AsyncTaskHandler(getContext()) {
			
			@Override
			protected void notifyUI(Message msg) {
				if(msg.what == MSG_STATUS_FINISH){
					
					list	= (List<ImageFile>)msg.obj ;
					
					adapter.notifyDataSetChanged() ;
				}
			}
			
			@Override
			protected void notifyError(Message msg) {
				
			}
			
			@Override
			protected void notifyCancel(Message msg) {
				
			}
		};
		
		AsyncTaskExecutor.execute(new AsyncTaskRunnable(handler) {
			
			@Override
			public Object onProcessing() {
				return FileScaner.scanImageFiles(getContext());
			}
		}) ;
	}

	private BaseAdapter adapter = new BaseAdapter() {
		
		@Override
		public View getView(int arg0, View view, ViewGroup arg2) {
			
			//System.out.println("zhouwei >> listview item=" + arg0);
			
			Holder holder = null ;
			
			if(view == null){
				holder	= new Holder() ;
				view	= mInflater.inflate(R.layout.image_item_layout, null);
				holder.view = (ImageView) view.findViewById(R.id.icon) ;
				holder.name	= (TextView) view.findViewById(R.id.icon_name);
				
				view.setTag(holder);
			} else {
				holder = (Holder)view.getTag() ;
			}
			
			ImageFile file = getItem(arg0);
			
			holder.name.setText(file.getFileName());
			
			//long enter = System.currentTimeMillis() ;
			
			ImageCooler.request(holder.view)
//			.withDecodeMaxWidth(16)
//			.withDecodeMaxHeight(16)
			.withFadeIn(true)
//			.withViewWidth(32)
//			.withViewHeight(32)
//			.withDecodeMaxWidth(32)
//			.withDecodeMaxHeight(32)
			.withDefault(R.drawable.ic_launcher)
			.withUrl(file.getFilePath()).fetch();
			
//			ImageCooler.request(holder.view,file.getFilePath(), op);
			
//			op.setDefaultResid(R.drawable.ic_launcher);
//			op.setWidth(32);
//			op.setHeight(32);
			//downloader.fetchImage(file.getFilePath(), holder.view, op);
			
			//System.out.println("zhouwei >> 时间 =" + (System.currentTimeMillis() - enter));
			
			return view ;
		}
		
		@Override
		public long getItemId(int arg0) {
			return arg0;
		}
		
		@Override
		public ImageFile getItem(int arg0) {
			return  list != null ? list.get(arg0) : null ;
		}
		
		@Override
		public int getCount() {
			return list != null ? list.size(): 0;
		}
		
		class Holder {
			
			TextView name ;
			ImageView view ;
		}
	};
}
